<template>
  <div>
    <el-dialog
      :title="info.isadd?'添加':'修改'"
      :visible.sync="info.isshow"
      @opened="opened"
      @closed="closed"
    >
      {{form}}
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="一级分类">
          <el-select v-model="form.first_cateid" placeholder="请选择" @change="changeonecate">
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="item in catelist"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="form.second_cateid" placeholder="请选择">
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="item in twocate"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="form.goodsname"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="市场价格">
          <el-input v-model="form.market_price"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="onChange"
          >
            <img v-if="form.img" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品规格">
          <el-select v-model="form.specsid" placeholder="请选择" @change="getattrs">
            <el-option
              :label="item.specsname"
              :value="item.id"
              v-for="item in specslist"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格属性">
          <el-select v-model="form.specsattr" placeholder="请选择" multiple>
            <el-option :label="item" :value="item" v-for="item in attrs" :key="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否新品">
          <el-switch v-model="form.isnew" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
        <el-form-item label="是否热卖">
          <el-switch v-model="form.ishot" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
        <el-form-item label="商品描述">
          <!-- 二、富文本编辑器  添加了条件渲染-->
          <div id="editor" v-if="info.isshow"></div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="info.isshow = false">取 消</el-button>
        <el-button type="primary" @click="add">{{info.isadd?'添加':'修改'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import E from "wangeditor";
import { mapGetters, mapActions } from "vuex";
import { reqgoodsadd, reqgoodsdit, reqcatelist } from "../../../request/api.js";
export default {
  components: {},
  data() {
    return {
      form: {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: "",
        isnew: 1,
        ishot: 1,
        status: 1
      },
      twocate: [],
      attrs: [],
      imageUrl: ""
    };
  },
  computed: {
    ...mapGetters({
      info: "goods/info",
      goodsinfo: "goods/goodsinfo",
      catelist: "cate/catelist",
      specslist: "specs/specslist"
    })
  },
  methods: {
    ...mapActions({
      reqgoodslist: "goods/goodslist",
      reqcatelist: "cate/catelist",
      reqspecslist: "specs/specslist",
      reqgoodsadd: "specs/goodsadd"
    }),
    cancel() {
      this.info.isshow = false;
      this.form = {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: "",
        isnew: 1,
        ishot: 1,
        status: 1
      };
      twocate = [];
      attrs = [];
      imageUrl = "";
    },
    changeonecate() {
      reqcatelist({ pid: this.form.first_cateid }).then(res => {
        this.twocate = res.data.list;
      });
    },
    // 商品规格数据
    getattrs() {
      let obj = this.specslist.find(item => item.id == this.form.specsid);
      this.attrs = obj ? obj.attrs : [];
      console.log(this.specslist);
    },
    add() {
      this.form.description = this.editor.txt.html();
      this.form.specsattr = this.form.specsattr.join(",");
      if (this.info.isadd) {
        reqgoodsadd(this.form).then(res => {
          if (res.data.code == 200) {
            this.reqgoodslist();
            this.info.isshow = false;
            this.cancel();
          }
        });
      } else {
        reqgoodsdit(this.form).then(res => {
          if (res.data.code == 200) {
            this.reqgoodslist();
            this.info.isshow = false;
          }
        });
      }
    },
    opened() {
      this.reqcatelist();
      //创建一个编辑器
      this.editor = new E("#editor");
      this.editor.create();
      //给编辑器赋值
      this.editor.txt.html(this.form.description);

      if (!this.info.isadd) {
        this.imageUrl = this.$pre + this.goodsinfo.img;
        this.form = this.goodsinfo;
        this.form.specsattr = this.goodsinfo.specsattr.split(",");
        this.changeonecate();
        this.getattrs();
        if (!!this.editor) {
          this.editor.txt.html(this.form.description);
        }
      }
    },
    onChange(e) {
      let file = e.raw;
      this.form.img = file;
      this.imageUrl = URL.createObjectURL(file);
    },
    closed() {
      this.form.description = this.editor.txt.html();
      if (!this.info.isadd) {
        this.form = {
          goodsname: "",
          attrs: "",
          status: 1
        };
      }
    }
    // addArr() {
    //   this.arr.push({ value: this.arr[0].value });
    // }
  },
  mounted() {
    this.reqspecslist();
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>